package com.gupao.vip.mic.javaapi;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

public class ZookeeperClientUtils {

	private final static String CONNECTSTRING="192.168.199.133:2181,192.168.199.134:2181," +
            "192.168.199.250:2181";
	public  static int sessionTimeout=5000;
	public static ZooKeeper getInstance() throws IOException, InterruptedException{
		final CountDownLatch connectLatch=new CountDownLatch(1);
		ZooKeeper zooKeeper=new ZooKeeper(CONNECTSTRING, sessionTimeout, new Watcher(){
			public void process(WatchedEvent event) {
				if(event.getState()==Event.KeeperState.SyncConnected){
					connectLatch.countDown();
				}
			}
		});
		connectLatch.await();
		return zooKeeper;
	}
	
	public static void main(String[] args) throws IOException, InterruptedException {
		/**
		 * 需要把zookeeper集群启动起来，才能在客户端得到zookeeper对象(坑)
		 */
		ZooKeeper zooKeeper = ZookeeperClientUtils.getInstance();
		System.out.println(zooKeeper);
	}
	
}
